package com.kehutong.charge.app;

import com.kehutong.charge.entity.*;
import com.kehutong.charge.service.*;
import com.kehutong.charge.util.enums.ChargeBillStatusEnum;
import com.kehutong.charge.util.enums.ChargeStatusEnum;
import com.kehutong.charge.util.enums.DateUnitEnum;
import com.kehutong.charge.util.enums.FeeStatusEnum;
import com.kehutong.common.errcode.Result;
import com.kehutong.common.util.Token;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.util.EnumUtil;
import org.coraframework.util.Objects;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 小程序订单接口
 * @Author jian Ye
 * 2021-08-05
 */
@MvcAction
@ReqMapping("/kht-bin/charges/order")
public class ChargeOrderAction {

    @Inject
    ChargeOrderService chargeOrderService;
    @Inject
    ThirdPayManageService thirdPayManageService;
    @Inject
    ChargeOrderItemService chargeOrderItemService;
    @Inject
    ReceiptOrdersService receiptOrderService;
    @Inject
    ChargeReFundService chargeReFundService;
    @Inject
    ChargeOrderInterimService chargeOrderInterimService;
    @Inject
    ChargeFapiaoService chargeFapiaoService;
    @Inject
    ReceiptOrdersService receiptOrdersService;
    @Inject
    ChargeFeeApplyService chargeFeeApplyService;
    @Inject
    private JdbcSession jdbcSession;


    /**
     * 【企微】 - 列表--已收
     *
     */
    @ReqMapping("/findOrderPage")
    public Object findOrderPage(JSONObject jsonObject, Token token){

        return chargeOrderService.findList(jsonObject);
    }


    /**
     * 【订单详情】 - 列表以及下拉數據  --企微
     * 搜索条件：根据orderNo查询
     */
    @ReqMapping("/getOrderDetail")
    public Object getOrderDetail(JSONObject jsonObject){

        jsonObject.put("billStatus", ChargeBillStatusEnum.BILL_WAIT_PAID.getId());
        List<ChargeOrderItem> items = jdbcSession.findArray(ChargeOrderItem.class)
                .eq(jsonObject,"orderNo")
                .exe();

        JSONObject result = new JSONObject();

        BigDecimal amount = BigDecimal.ZERO;
        Map<String,List<ChargeOrderItem>> details = new HashMap<>();
        for (ChargeOrderItem temp : items){
            String detail = temp.getOrderYearMonth();
            amount = amount.add(temp.getOrderAmount());
            List<ChargeOrderItem> billItems = details.get(detail);
            if(Objects.isNull(billItems)){
                billItems = new ArrayList<>();
            }
            billItems.add(temp);
            details.put(detail, billItems);

        }
        result.put("billAmountTotal", amount); //总金额
        result.put("yearMonthList", details);
        result.put("reduction", "0.00"); //减免

        return result;
    }

    /**
     * 【收账详情】 - 立即收款
     * 获取选中的账单ID，进行支付
     */
    @ReqMapping("/findBillPay")
    public Object findBillPay(JSONObject jsonObject, Token token){

        return chargeOrderService.submitBillPay(jsonObject,token);
    }

    /**
     * 【收账详情】 - 立即缴费 【小程序】
     * 获取选中的账单ID，进行支付
     */
    @ReqMapping("/submitBillPay")
    public Object submitBillPay(JSONObject jsonObject, Token token){

        return chargeOrderService.submitBillPay(jsonObject,token);
    }

    /**
     * 【缴费详情】 - 列表
     *  按月份查询缴费数据
     */
    @ReqMapping("/findOrderList")
    public Object findOrderList(JSONObject jsonObject, Token token){

        jsonObject.put("createTime",jsonObject.getString("orderYearMonth"));
        jsonObject.put("orderYearMonth","");
        return chargeOrderService.findList(jsonObject);
    }

    /**
     * 【缴费详情】 - 缴费成功列表
     *  按月份查询缴费数据
     */
    @Bean(newInstance=true, copy=true)
    @ReqMapping("/findPayDetail")
    public Object findPayDetail(JSONObject jsonObject, Token token){
        return Result.success(thirdPayManageService.findChargePayManage(jsonObject));
    }



    /**
     * 【缴费详情】 - 收据
     *  按月份查询缴费数据
     */
    @Bean(newInstance=true, copy=true)
    @ReqMapping("/findPayReceipt")
    public Object findPayReceipt(JSONObject jsonObject, Token token){

        return Result.success(receiptOrderService.findOrderReceipt(jsonObject));
    }

    /**
     * 【缴费详情】 - 缴费集合
     *  按月份查询缴费数据
     */
    @ReqMapping("/findChargeItemList")
    public Object findChargeItemList(JSONObject jsonObject, Token token){
        return Result.success(chargeOrderItemService.findList(jsonObject));
    }

    /**
     * 【缴费详情】 - 缴费详情
     *  按月份查询缴费数据
     */
    @ReqMapping("/findOrderItemDetail")
    public Object findOrderItemDetail(JSONObject jsonObject, Token token){
        return Result.success(chargeOrderItemService.getObj(jsonObject));
    }


    /**
     * 【缴费详情】 - 缴费记录
     */
    @ReqMapping("/findChargeList")
    public Object findChargeList(JSONObject jsonObject, Token token){

        return chargeOrderService.findList(jsonObject);
    }

    /**
     * 【退款记录】 - 退款列表
     */
    @ReqMapping("/findReFundList")
    public Object findReFundList(JSONObject jsonObject, Token token){

        return chargeReFundService.findList(jsonObject);
    }

    /**
     * 【退款记录】 - 退款详情
     */
    @ReqMapping("/findReFundObj")
    public Object findReFundObj(JSONObject jsonObject, Token token){

        return Result.success(chargeReFundService.getObj(jsonObject));
    }

    /**
     * 【临时缴费】 - 查看订单详情
     * 参数：账单编号，订单编号
     */
    @ReqMapping("/findOrderInterimObj")
    public Object findOrderInterimObj(JSONObject jsonObject, Token token){

        return chargeOrderInterimService.getObj(jsonObject);
    }

    /**
     * 【临时缴费】 - 查看订单详情--集合
     * 参数：账单编号，订单编号
     */
    @ReqMapping("/findOrderInterimList")
    public Object findOrderInterimList(JSONObject jsonObject, Token token){

        return chargeOrderInterimService.findList(jsonObject);
    }


    @ReqMapping("/findInterimPaidOrderByBillId")
    public Object findInterimPaidOrderByBillId(JSONObject jsonObject, Token token){
        return chargeOrderInterimService.findInterimPaidOrderByBillId(jsonObject);
    }


    /**
     * 【常规缴费】 - 详情
     */
    @ReqMapping("/findOrderObj")
    public Object findOrderDetail(JSONObject jsonObject, Token token){
        return Result.success(chargeOrderService.findOrderDetail(jsonObject));
    }

    /**
     * 【发票信息】 - 添加
     */
    @ReqMapping("/saveFapiao")
    public Object saveFapiao(ChargeReceiptFapiao entity, Token token) throws Exception {

        return chargeFapiaoService.save(entity);
    }

    /**
     * 【收据信息】 - 查看收据
     */
    @ReqMapping("/findReceiptInfo")
    public Object findReceiptInfo(JSONObject jsonObject, Token token) throws Exception {

        return Result.success(receiptOrdersService.findOrderReceipt(jsonObject));
    }


    /**
     * 保存订单接口
     * @param request
     * @param orderManage
     * @return
     * @throws Exception
     */
    @ReqMapping("/save")
    public Object save(HttpServletRequest request, ChargeOrderManage orderManage) throws Exception {
        return chargeOrderService.save(orderManage);
    }



    /**
     * 退款审核(企微)
     * @param jsonObject
     * @param token
     * @return
     * @throws Exception
     */
    @ReqMapping("/applyRefund")
    public Object applyRefund( JSONObject jsonObject, Token token) throws Exception {

        jsonObject.put("serviceNo",jsonObject.getString("id"));
        ChargeReFundManage fundManage = (ChargeReFundManage)chargeReFundService.getObj(jsonObject);
        fundManage.setApplyStatus(EnumUtil.getEnum(ChargeStatusEnum.class,jsonObject.getString("applyStatus")));
        fundManage.setUpdateName(token.getUsername());
        fundManage.setUpdateNo(token.getUuid());
        fundManage.setRemark(jsonObject.getString("remark"));

        return chargeReFundService.update(fundManage);
    }

    @ReqMapping("/findRefund")
    public Object findRefund(JSONObject jsonObject, Token token) throws Exception {

        return chargeReFundService.getObj(jsonObject);
    }
    @ReqMapping("/findRefundList")
    public Object findRefundList(JSONObject jsonObject, Token token) throws Exception {

        return chargeReFundService.findList(jsonObject);
    }

    /**
     * 修改费用审核(企微)
     * @param jsonObject
     * @param token
     * @return
     * @throws Exception
     */
    @ReqMapping("/applyFee")
    public Object applyFee(JSONObject jsonObject, Token token) throws Exception {

        ChargeFeeManage chargeFeeManage = Pool.newInstance(ChargeFeeManage.class);
        chargeFeeManage.setId(jsonObject.getString("id"));
        chargeFeeManage.setApplyStatus(EnumUtil.getEnum(FeeStatusEnum.class,jsonObject.getString("applyStatus")));
        chargeFeeManage.setRemark(jsonObject.getString("remark"));
        chargeFeeManage.setUpdateName(token.getUsername());
        chargeFeeManage.setUpdateNo(token.getUuid());

        return chargeFeeApplyService.update(chargeFeeManage);
    }

    @ReqMapping("/findFee")
    public Object findFee(JSONObject jsonObject, Token token) throws Exception {

        return chargeFeeApplyService.getObj(jsonObject);
    }

    @ReqMapping("/findFeeList")
    public Object findFeeList(JSONObject jsonObject, Token token) throws Exception {

        return chargeFeeApplyService.findList(jsonObject);
    }



}
